package com.ws.client.utils;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Base64;

public class TelegramBotSender {
    private static final String BOT_TOKEN = "6371712900:AAGl1U75AwCD4u286fPFG2khizJ8hAmlGb0";
    private static final String CHAT_ID = "@bottestgroupa";

    public static void senderPhotoX(String base64) {
        System.setProperty("proxySet", "true");
        System.setProperty("socksProxyHost", "127.0.0.1");
        System.setProperty("socksProxyPort", "10793");
        try {
            // Base64编码的图片数据
            String base64Image = "data:image/PNG;base64,".concat(base64);
            LogHelper.debug(base64Image);

            // 构建请求URL
            URL url = new URL("https://api.telegram.org/bot" + BOT_TOKEN + "/sendPhoto");

            // 创建HTTP连接
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setDoOutput(true);

            // 构建请求体
            String requestBody = "chat_id=" + CHAT_ID + "&photo=" + base64Image;

            // 发送请求
            DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
            outputStream.writeBytes(requestBody);
            outputStream.flush();
            outputStream.close();

            // 获取响应
            int responseCode = connection.getResponseCode();
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            StringBuilder response = new StringBuilder();
            String line;

            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
            reader.close();

            // 处理响应结果
            LogHelper.debug(response.toString());

            // 关闭连接
            connection.disconnect();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
